#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Motor.h"
#include "Tcrt5000.h"
#include "Timer.h"

int8_t Speed;						//定义速度变量
uint8_t LEFT1,RIGHT1,MIDDLE;		//定义三个红外对管
uint8_t Min;						//定义时间分钟
uint8_t Sec;						//定义时间秒

int main(void)
{
	/*模块初始化*/
	OLED_Init();		//OLED初始化
	Motor_Init();		//直流电机初始化
	Tcrt5000_Init();	//红外对管初始化
	Timer3_Init();		//定时器3初始化
	
	
	/*显示静态字符串*/
	OLED_ShowString(2, 1, "Time:");		
	OLED_ShowString(2, 8, ":");
	
	while (1)
	{
		/*显示运行时间*/
		OLED_ShowNum(2, 6, Min, 2);
		OLED_ShowNum(2, 9, Sec, 2);

		/*重新命名各个传感器*/
		RIGHT1 = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12);
		LEFT1 = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11);
		MIDDLE = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14);
						
		
		/*沿黑线直行*/
		if( MIDDLE == 1)
		{
			Motor_LeftSetSpeed(30);
			Motor_RightSetSpeed(30);
		}
		
		/*沿黑线左转*/
		else if(LEFT1 == 1)		
		{
			Motor_LeftSetSpeed(-30);
			Motor_RightSetSpeed(30);
		}
		
		/*沿黑线右转*/
		else if( RIGHT1 == 1)		
		{
			Motor_LeftSetSpeed(30);
			Motor_RightSetSpeed(-30);
		}
				
		/*未探测到黑线的时候直走（防止未检测到黑线时不动）*/
		else if(LEFT1 == 0 && MIDDLE == 0 && RIGHT1 == 0)			
		{	
			Motor_LeftSetSpeed(30);
			Motor_RightSetSpeed(30);
		}
		
		/*检测到横线停止*/
		else if(LEFT1 == 1 && MIDDLE == 1 && RIGHT1 == 1)			
		{	
			Motor_LeftSetSpeed(0);
			Motor_RightSetSpeed(0);
		}
	}
}

void TIM3_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
	{
		Sec ++;
		if(Sec == 60)
		{
			Sec = 0;
			Min ++;
		}
		TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
	}
}

